#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.bean.vo;

import ${package}.common.validator.NotIncloud;
import ${package}.common.validator.ShutterIsland;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Description: QueryBean
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Data
public class QueryBean {

    /**
     * 当前页码
     */
    @Min(value = 0, message = "需要访问的页码不能为负数")
    @Max(value = Integer.MAX_VALUE - 1, message = "需要访问的页面过大")
    @ApiModelProperty("当前页码")
    private Integer current;

    /**
     *每页条数
     */
    @Min(value = 5, message = "每个表格最小的条数为5条")
    @Max(value = 100, message = "每个表格最大的条数为200条")
    @ApiModelProperty("每页条数")
    private Integer pageSize;

    /**
     *是否开启排序
     */
    @ApiModelProperty("是否开启排序")
    private Boolean sort;

    /**
     *排序字段
     */
    @Length(max = 32)
    @NotIncloud(law = {"_", "-", "*", "/", "&"})
    @ApiModelProperty("排序字段")
    private String sortField;

    /**
     *排序规则
     */
    @ShutterIsland(teddy = {"desc", "DESC", "asc", "ASC"})
    @ApiModelProperty("排序规则")
    private String sortType;

    /**
     *是否查询
     */
    @ApiModelProperty("是否查询")
    private Boolean search;

    public void setFilters(List<List<Constraint>> filters) {
        if(null != filters) {
            filters.forEach(item -> item.removeIf(Objects::isNull));
            this.filters = filters;
            this.search = Boolean.TRUE;
        }
    }

    /**
     *查询规则
     */
    @Valid
    @ApiModelProperty("查询规则")
    private List<List<Constraint>> filters = Lists.newArrayList();

    public QueryBean() {
    }

    public QueryBean(Integer current, Integer pageSize) {
        this.current = current;
        this.pageSize = pageSize;
    }

    /**
     * Description:
     * <一个或个Constraint做and运算>
     * @author wupanhua
     * @date 13:51 2019/8/8
     * @param constraint 1
     **/
    public void and(Constraint ...constraint) {

        if (this.filters == null) {
            this.filters = new ArrayList<>();
        }

        List<Constraint> lc = new ArrayList<>();
        for (Constraint c: constraint) {
            if(null!=c) {
                lc.add(c);
                this.filters.add(lc);
            }
        }
        this.search = Boolean.TRUE;
    }

    /**
     * Description:
     * <一个或多个List<Constraint>做and运算>
     * @author wupanhua
     * @date 13:51 2019/8/8
     * @param constraint 1
     **/
    public void and(List<Constraint> ...constraint) {

        if (this.filters == null) {
            this.filters = new ArrayList<>();
        }
        Collections.addAll(this.filters, constraint);
        this.search = Boolean.TRUE;
    }

    /**
     * Description:
     * <一个或多个Constraint做or运算>
     * @author wupanhua
     * @date 13:52 2019/8/8
     * @param constraint 1
     **/
    public void or(Constraint ...constraint) {

        if (this.filters == null) {
            this.filters = new ArrayList<>();
        }

        List<Constraint> lc = new ArrayList<>();
        Collections.addAll(lc, constraint);
        this.filters.add(lc);
        this.search = Boolean.TRUE;
    }

    /**
     * Description:
     * <and一个sql>
     * @param sql 1
     * @author wenxiaopeng
     * @date 16:07 2021/05/19
     **/
	public void and(String sql) {
		// FIXME 防sql注入
		if (this.filters == null) {
			this.filters = new ArrayList<>();
		}
		List<Constraint> lc = new ArrayList<>();
		lc.add(Constraint.builder().sql(sql).build());
		this.filters.add(lc);
		this.search = Boolean.TRUE;
	}
}
